package ImageProcessors;


import java.util.List;

import org.apache.commons.lang3.time.DurationFormatUtils;
import org.apache.commons.lang3.time.StopWatch;

import pdi.IImg2ImgOperation;
import pdi.IO.ImgReader;
import pdi.IO.ImgWriter;
import pdi.core.Image;




public class BatchProcessor {
	
	private static BatchProcessor instance = null;

	protected BatchProcessor() {}

	public static BatchProcessor getInstance() {
		if(instance == null) {
			instance = new BatchProcessor();
		}
		return instance;
	}

	public void process(ImgReader reader, ImgWriter writer, IImg2ImgOperation... operations){
		
		StopWatch stp = new StopWatch();
		stp.start();
		int imgCount = 0;
		
		
		//open the archives
		reader.open();		
		writer.open();
		
		//read images sequentialy
		Image img = null;
		while((img = reader.readNext()) != null){
			
			//perform a sequence of operations
			for (IImg2ImgOperation op : operations) {
				if(op != null)
					img = op.execute(img);
			}
			
			//write an image
			writer.writeNext(img);
			
			if((++imgCount) % 100 == 0){
				System.out.println(imgCount + " processed in " + DurationFormatUtils.formatDuration(stp.getTime(),"HH:mm:ss.S") + " (HH:mm:ss.S)");
			}
		}		
		
		//close the archives
		reader.close();
		writer.close();
	}
	

}
